import {FormProps, FormSchema} from '/@/components/Table';

import {BasicColumn} from '/@/components/Table/src/types/table';
import {getJulyDictItemListByTypeApi} from '/@/api/admin/julyDictItemApi';
import {uploadApi} from '/@/api/sys/upload';
import {Tag} from "ant-design-vue";
import {Tinymce} from '/@/components/Tinymce/index';
import {h} from 'vue';


//表格显示列
export function getBasicColumns(): BasicColumn[] {
  // @ts-ignore
  return [
    {
      title: 'ID',
      dataIndex: 'articleId',
      fixed: 'left',
      width: 100,
    },
    {
      title: '文章标题',
      dataIndex: 'articleTitle',
      width: 150,

    },
    {
      title: '排序',
      dataIndex: 'indexOrder',
      width: 100,
    },
    {
      title: '浏览量',
      dataIndex: 'views',
      width: 100,
    },
    {
      title: '是否为首页主图',
      dataIndex: 'indexMainFlag',
      width: 150,
      //sorter: true,
      //defaultHidden: true,
      customRender: ({record}) => {
        let indexMainFlag = record.indexMainFlag;
        let color = ''
        switch (indexMainFlag) {
          case '0':
            indexMainFlag = '是';
            color = 'green'
            break;
          case '1':
            indexMainFlag = '否';
            color = 'purple'
            break;
        }
        return h(Tag, {color: color}, () => indexMainFlag);
      },
    },
    {
      title: '文章类别',
      dataIndex: 'articleType',
      width: 150,
      //sorter: true,
      //defaultHidden: true,
      customRender: ({record}) => {
        let articleType = record.articleType;
        let color=''
        switch (articleType) {
          case 'INFORMATION':
            articleType = '新闻资讯';
            color = 'purple'
            break;
          case 'NOTICE':
            articleType = '公告';
            color = 'orange'
            break;
          case 'INFORM':
            articleType = '通知';
            color = 'red'
            break;
          default:
        }
        return h(Tag, {color: color}, () => articleType);
      },
    },
    {
      title: '推送状态',
      width: 100,
      dataIndex: 'pushStatus',
      customRender: ({record}) => {
        let pushStatus = record.pushStatus;
        let color = ''
        switch (pushStatus) {
          case '0':
            pushStatus = '推送';
            color = 'green'
            break;
          case '1':
            pushStatus = '不推送';
            color = 'purple'
            break;
        }
        return h(Tag, {color: color}, () => pushStatus);
      },
    },

  ];
}

export function getFormConfig(): Partial<FormProps> {
  return {
    labelWidth: 150,
    baseColProps: {lg: 6, md: 24},
    schemas: formParamSchema,
  };
}


//编辑表单
export const formParamSchema: FormSchema[] = [
  {
    field: 'articleTitle',
    label: '文章标题',
    component: 'Input',
  },
  {
    field: 'articleType',
    label: '文章类别',
    component: 'ApiSelect',
    componentProps: {
      api: getJulyDictItemListByTypeApi,
      params: {type: 'XH_ARTICLE_TYPE'},
      labelField: 'label',
      valueField: 'value',
    },
  },
];

//编辑表单
export const formSchema: FormSchema[] = [
  {
    field: 'articleTitle',
    label: '文章标题',
    component: 'Input',
    required: true,
  },
  {
    field: 'articleType',
    label: '文章类别',
    component: 'ApiSelect',
    componentProps: {
      api: getJulyDictItemListByTypeApi,
      params: {type: 'XH_ARTICLE_TYPE'},
      labelField: 'label',
      valueField: 'value',
    },
    required: true,
  },
  {
    field: 'outline',
    label: '概要',
    component: 'InputTextArea',
    required: true,
  },
  {
    field: 'articleContent',
    component: 'Input',
    label: '文章内容',
    defaultValue: '',
    rules: [{required: true}],
    render: ({model, field}) => {
      return h(Tinymce, {
        value: model[field],
        onChange: (value: string) => {
          model[field] = value;
        },
      });
    },
  },

  {
    field: 'indexOrder',
    label: '排序',
    component: 'InputNumber',
    required: true,
  },
  {
    field: 'titleTime',
    label: '发布时间',
    component: 'DatePicker',
    required: true,
    componentProps: {
      valueFormat: 'YYYY-MM-DD',
      format: 'YYYY-MM-DD',
    },
  },
  {
    field: 'files',
    label: '附件',
    component: 'Upload',
    componentProps: {
      api: uploadApi,
      maxSize: 1,
    },
  },
  {
    field: 'coverImage',
    label: '封面图',
    component: 'Upload',
    componentProps: {
      api: uploadApi,
      accept: ['jpg', 'png', 'jpeg'],
      maxNumber: 1,
      maxSize: 1,
      helpText: '请上传png/jpg 等图片文件，文件大小小于1M,宽高比为7:4',
    },
  },
  {
    field: 'indexMainFlag',
    label: '首页主图新闻',
    component: 'RadioButtonGroup',
    defaultValue: '0',
    required: true,
    componentProps: {
      options: [
        {label: '是', value: '0'},
        {label: '否', value: '1'},
      ],
    },
  },
  {
    field: 'pushStatus',
    label: '推送状态',
    component: 'RadioButtonGroup',
    defaultValue: '0',
    required: true,
    componentProps: {
      options: [
        {label: '推送', value: '0'},
        {label: '不推送', value: '1'},
      ],
    },
  },

];
